/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.StringUtils;
import com.rf.sys.utils.UuidUtil;
import com.rf.sys.dao.SysLogMapper;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.SysLog;
import com.rf.sys.model.SysLogExample;
import com.rf.sys.service.SysLogService;

@Service("sysLogService")
public class SysLogServiceImpl implements SysLogService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private SysLogMapper sysLogMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param sysLogId
	 * @return SysLog
	 */
   	@Override
	public SysLog getSysLogByID(String id) throws Exception{
		// TODO Auto-generated method stub
		return sysLogMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据SysLog获取数据
	 * @param SysLog
	 * @return List<SysLog>
	 */
	@Override
	public List<SysLog> getSysLog(SysLog sysLog) throws Exception{
		// TODO Auto-generated method stub
		SysLogExample example = new SysLogExample();
		com.rf.sys.model.SysLogExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<SysLog> list = sysLogMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<SysLog>
	 */
	@Override
	public List<SysLog> getSysLog() throws Exception{
		// TODO Auto-generated method stub
		SysLogExample example = new SysLogExample();
		com.rf.sys.model.SysLogExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return sysLogMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param SysLog
	 * @return
	 */
	@Override
	public void addSysLog(SysLog sysLog) throws Exception{
		// TODO Auto-generated method stub
		sysLog.setId(UuidUtil.get32UUID());
		sysLogMapper.insert(sysLog);
	}

	/**
	 * 修改数据
	 * @param SysLog
	 * @param
	 */
	@Override
	public void updateSysLog(SysLog sysLog) throws Exception{
		// TODO Auto-generated method stub
		sysLogMapper.updateByPrimaryKeySelective(sysLog);
	}
	
	/**
	 * 删除数据
	 * @param sysLogId
	 * @param
	 */
	@Override
	public void deleteSysLog(String id) throws Exception{
		// TODO Auto-generated method stub
		sysLogMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 获取数据分页
	 * @param SysLog
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getSysLogPage(SysLog sysLog,PageUtils pageUtils) throws Exception{
		SysLogExample example = new SysLogExample();
		com.rf.sys.model.SysLogExample.Criteria criteria= example.createCriteria();
		
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<SysLog> list = sysLogMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	
	/**
	 * 系统日志查询
	 */
	public Map getSysLogsPage(SysLog sysLog,PageUtils pageUtils) throws Exception{
		StringBuffer sql =new StringBuffer("select * from (select g.user_id,g.methods methods,g.user_ip,g.oper_time,'101701' logtype from sys_log g union select l.account_name user_id,'用户登录' methods,l.login_ip user_ip,l.login_time oper_time,'101702' logtype   from SYS_USER_LOGIN_LOG l) t where 1=1");
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		List<Map<String, Object>> list=sqlAdapterMapper.selectSQL(sql.toString());
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	
	}
}

